package com.lv.leyou.common.advice;

import com.lv.leyou.common.exception.LyException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
@Slf4j
public class CommonLogAdvice {
    @Around("within(@org.springframework.stereotype.Service *) || within(com.baomidou.mybatisplus.extension.service.IService+)")
    public Object handleExceptionLog(ProceedingJoinPoint jp)throws Throwable {
        try {
            log.debug("{}方法准备调用，参数：{}",jp.getSignature(), Arrays.toString(jp.getArgs()));
            long a =System.currentTimeMillis();
            //调用切点方法
            Object result = jp.proceed();
            //记录方法结束日志
            log.debug("{}方法调用成功，执行耗时{}", jp.getSignature(), System.currentTimeMillis() - a);
            return result;
        } catch (Throwable throwable) {
            log.error("{}方法执行失败，原因：{}",jp.getSignature(),throwable.getMessage());
            //判断异常是否是LyEXCEPTION
            if (throwable instanceof LyException) {
                throw throwable;
            }else {
                //如果不是转换成LyException
                throw new LyException(500, throwable);
            }
        }
    }
}
